package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.dao.UserDao;
import com.baizhi.cmfz.entity.User;
import com.baizhi.cmfz.entity.UserDTO;
import com.baizhi.cmfz.service.UserService;
import com.baizhi.cmfz.util.ThreadPollUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Override
    public Map showPageUser(int page, int limit) {
        int offset=(page-1)*limit;
        List<User> list=userDao.showPageUser(offset,limit);
        int count=userDao.showCount();
        Map map=new HashMap();
        if(count>0){
            map.put("data",list);
            map.put("count",count);
            map.put("code",0);
            map.put("msg","");
        }
        return map;
    }

    @Override
    public List<UserDTO> showSex( ) {
        return userDao.showSex();
    }

    @Override
    public List<Map> showSexs() {
        return userDao.showSexs();
    }

    @Override
    public List<Map> showCity() {
        return userDao.showCity();
    }

    @Override
    public Map showDate() {
        Map map=new HashMap();
        int count1=userDao.showDate(0);
        int count2=userDao.showDate(1);
        int count3=userDao.showDate(2);
        map.put("count1",count1);
        map.put("count2",count2);
        map.put("count3",count3);
        return map;
    }

    @Override
    public Map selectByThread() throws InterruptedException {
        Map map=new HashMap();
        CountDownLatch countDownLatch=new CountDownLatch(3);
        ThreadPoolExecutor executor=ThreadPollUtil.executor;
        executor.submit(new Runnable() {
            @Override
            public void run() {
                Map map1=showDate();
                map.put("count",map1);
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
                List<Map> map2=showSexs();
                map.put("sex","map2");
                countDownLatch.countDown();
            }
        });
        executor.submit(new Runnable() {
            @Override
            public void run() {
               List<Map> map3=showCity();
                map.put("city",map3);
                countDownLatch.countDown();
            }
        });
        countDownLatch.await();
        return map;
    }
}
